layui.use(['table', 'admin', 'ax', 'func','tree'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var tree = layui.tree;

    /**
     * 装修货品分类表管理
     */
    var BtyZxHuopinFenlei = {
        tableId: "btyZxHuopinFenleiTable",
        pid:""
    };

    /**
     * 初始化表格的列
     */
    BtyZxHuopinFenlei.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'code', sort: true, title: '货品分类编码'},
            {field: 'pName', sort: true, title: '上级名称'},
            {field: 'name', sort: true, title: '货品分类名称'},
            {field: 'createUser', hide:true, sort: true, title: '创建人'},
            {field: 'createName', sort: true, title: '创建人'},
            {field: 'createTime', sort: true, title: '创建时间'},
            // {field: 'updateUser', sort: true, title: '更新人'},
            // {field: 'updateTime', sort: true, title: '更新时间'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    BtyZxHuopinFenlei.search = function () {
        var queryData = {};
        queryData['pid'] = BtyZxHuopinFenlei.pid;
        queryData['name'] = $('#name').val();

        table.reload(BtyZxHuopinFenlei.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 选择科目时
     */
    BtyZxHuopinFenlei.onClickNode = function (obj) {
        obj.data.checked="true";
        BtyZxHuopinFenlei.pid = obj.data.id;
        BtyZxHuopinFenlei.search();
    };

    /**
     * 弹出添加对话框
     */
    BtyZxHuopinFenlei.openAddDlg = function () {
        if(BtyZxHuopinFenlei.pid==""){
            BtyZxHuopinFenlei.pid=0;
        }
        if($("#code").val()==""){
            Feng.error("请输入货品分类编码!");
            return false;
        }
        if($("#names").val()==""){
            Feng.error("请输入货品分类名称!");
            return false;
        }
        var ajax = new $ax(Feng.ctxPath + "/btyZxHuopinFenlei/addItem", function (data) {
            if(data.code==1){
                Feng.error("添加失败!" + data.message);
                return;
            }
            Feng.success("添加成功！");
            table.reload(BtyZxHuopinFenlei.tableId);
            BtyZxHuopinFenlei.kemutreeinit();
        }, function (data) {
            Feng.error("添加失败！" + data.responseJSON.message)
        });
        ajax.set("name",$("#names").val());
        ajax.set("code",$("#code").val());
        ajax.set("pid",  BtyZxHuopinFenlei.pid);
        ajax.start();
        return false;
        // func.open({
        //     title: '添加装修货品分类',
        //     content: Feng.ctxPath + '/btyZxHuopinFenlei/add',
        //     tableId: BtyZxHuopinFenlei.tableId
        // });
    };

     /**
      * 点击编辑
      *
      * @param data 点击按钮时候的行数据
      */
      BtyZxHuopinFenlei.openEditDlg = function (data) {
          // func.open({
          //     title: '修改装修货品分类',
          //     content: Feng.ctxPath + '/btyZxHuopinFenlei/edit?id=' + data.id,
          //     tableId: BtyZxHuopinFenlei.tableId
          // });
          window.location.href = Feng.ctxPath + '/btyZxHuopinFenlei/edit?id=' + data.id;
      };


    /**
     * 导出excel按钮
     */
    BtyZxHuopinFenlei.exportExcel = function () {
        var checkRows = table.checkStatus(BtyZxHuopinFenlei.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    BtyZxHuopinFenlei.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/btyZxHuopinFenlei/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(BtyZxHuopinFenlei.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + BtyZxHuopinFenlei.tableId,
        url: Feng.ctxPath + '/btyZxHuopinFenlei/list',
        page: true,
        height: "full-90",
        limit:20,
        cellMinWidth: 100,
        cols: BtyZxHuopinFenlei.initColumn()
    });

    //初始化左侧门店树

    BtyZxHuopinFenlei.kemutreeinit=function () {
        var ajax = new $ax(Feng.ctxPath + "/btyZxHuopinFenlei/layuiTree", function (data) {
            tree.render({
                elem: '#kemuTree',
                data: data,
                click: BtyZxHuopinFenlei.onClickNode,
                onlyIconControl: true
            });
        }, function (data) {
        });
        ajax.start();
    };

    BtyZxHuopinFenlei.kemutreeinit();

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        BtyZxHuopinFenlei.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

    BtyZxHuopinFenlei.openAddDlg();

    });

    // 导出excel
    $('#btnExp').click(function () {
        BtyZxHuopinFenlei.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + BtyZxHuopinFenlei.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            BtyZxHuopinFenlei.openEditDlg(data);
        } else if (layEvent === 'delete') {
            BtyZxHuopinFenlei.onDeleteItem(data);
        }
    });

    $(function () {
        var panehHidden = false;
        if ($(this).width() < 769) {
            panehHidden = true;
        }
        $('#myContiner').layout({initClosed: panehHidden, west__size: 260});
    });

});

